﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Linq;
using System.Xml;

namespace Prologis.Shifter.Test.Inspection
{
    /// <summary>
    /// Summary description for RecurseXmlInspection
    /// </summary>
    [TestClass]
    public class RecurseXmlInspection
    {
        public RecurseXmlInspection()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void TestMethod1()
        {
            //
            // TODO: Add test logic here
            //
        }


        [TestMethod]
        public void RecursionTestMethod1()
        {
            //
            // TODO: Add test logic here
            //

            string fileContent =
@"  <configuration>
        <sub1>
            <sub1Sub1>
            </sub1Sub1>
        </sub1>
        <sub2>
            <sub2Sub1>
            </sub2Sub1>        
            <fields>
                <field key='true' index ='0' name='ARTNR1'  source='File' pos='0' length='10' enabled='true'/>
                <field key='true' index ='1' name='ARTNR2'  source='File' pos='0' length='10' enabled='true'/>
                <field key='true' index ='2' name='ARTNR3'  source='File' pos='0' length='10' enabled='true'/>
                <field key='true' index ='3' name='ARTNR4'  source='File' pos='0' length='10' enabled='true'/>
            </fields>
        </sub2>
   </configuration>";

            XElement root = XElement.Parse(fileContent);


            //XElement fieldsElement = FindXmlNode(root, "Fields");

            XElement node = GetElementById("fields", root);
            string s = "";

        }


        public XElement GetElementById(string name, XElement node)
        {

            if (node.Name.LocalName == name)
            {
                return node;
            }
            else
            {
                foreach (XElement childNode in node.Elements())
                {
                    //if (node.Name.LocalName == name)
                    //{
                    //    return node;
                    //}


                    return GetElementById(name, childNode);

                }
                return null;
            }




            //return null;
        }

        private XElement HandleElement(XElement nodeList, string name)
        {
            if (nodeList.Name.LocalName.Equals(name, StringComparison.InvariantCultureIgnoreCase))
            {
                return nodeList;
            }
            foreach (XElement ChildNode in nodeList.Elements())
            {
                if (ChildNode.Name.LocalName.Equals(name, StringComparison.InvariantCultureIgnoreCase))
                {
                    return ChildNode;
                }
                else
                    if (ChildNode.HasElements)
                    {
                        HandleElement(ChildNode, name);
                    }
                //HandleAttributes( ChildNode );
            }

            return null;
        }




        //public static XElement FindElement(XElement element, string elementName)
        //{
        //    XElement result = element;

        //    if (!element.HasElements)
        //    {
        //        // element is child with no descendants
        //    }
        //    else
        //    {
        //        foreach (var child in element.Elements())
        //        {
        //            if (child.Name.LocalName.Equals(elementName, StringComparison.InvariantCultureIgnoreCase))
        //            {
        //                return child;
        //            }
        //            else
        //            {
        //                FindElement(child, elementName);
        //            }
        //        }
        //    }
        //    //throw new Exception(String.Format("The XElement {0} does not have the child {1}!", element.Name.LocalName, elementName));
        //}

        void Process(XElement element, int depth)
        {
            // For simplicity, argument validation not performed

            if (!element.HasElements)
            {
                // element is child with no descendants
            }
            else
            {
                // element is parent with children

                depth++;

                foreach (var child in element.Elements())
                {
                    Process(child, depth);
                }

                depth--;
            }
        }


        private static void RecurseXmlDocument(XmlNode root)
        {
            if (root.HasChildNodes)
                RecurseXmlDocument(root.FirstChild);
            if (root.NextSibling != null)
                RecurseXmlDocument(root.NextSibling);
        }

        public static XElement FindXmlNode(XElement root, string name)
        {
            if (root.Name.LocalName == name)
            {
                return root;
            }
            else
            {
                foreach (XElement node in root.Elements())
                {
                    if (node.Name.LocalName == name)
                    {
                        return node;
                    }
                    else
                    {
                        return FindXmlNode(node, name);
                    }

                    //if (result != null)
                    //{
                    //    return result;
                    //}
                }
            }
            return null;
        }


        //public static XmlNodeFormatter FindXmlNode(XmlNodeFormatter root, string name)
        //{
        //    if (root.tagName == name)
        //    {
        //        return root;
        //    }
        //    else
        //    {
        //        foreach (XmlNodeFormatter node in root.childNodes)
        //        {
        //            XmlNodeFormatter result = FindXmlNode(XmlNodeFormatter, name);
        //            if (result != null)
        //            {
        //                return result;
        //            }
        //        }
        //    }
        //    return null;
        //}



        private void HandleElement(XmlNodeList nodeList)
        {
            foreach (XmlNode ChildNode in nodeList)
            {
                if ((ChildNode.HasChildNodes))
                {
                    HandleElement(ChildNode.ChildNodes);
                }
                //HandleAttributes( ChildNode );
            }
        }



    }
}


